﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ReadFromAll.Models;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;

namespace ReadFromAll.Service
{
    public class StuServices
    {
        /// <summary>
        /// 读取数据库中数据
        /// </summary>
        /// <returns></returns>
        public static List<Student> ReadToList()
        {
            dbEntities db = new dbEntities();
            var students = db.Student;
            return students.ToList();
        }
        /// <summary>
        /// 输出平均成绩，最高最低分的信息
        /// </summary>
        /// <param name="students"></param>
        public static void Output(List<Student> students)
        {
            //得到平均成绩
            double average = students.Average(m => m.Grade);
            Console.WriteLine("这个班的平均分是：" + average);
            //得到最高分的信息
            int max = students.Max(m => m.Grade);
            foreach (Student student in students)
            {
                if (student.Grade == max)
                {
                    Console.WriteLine("最高分同学的姓名：" + student.Name);
                    Console.WriteLine("学号：" + student.No);
                    Console.WriteLine("成绩：" + student.Grade);
                    Console.WriteLine("出生日期：" + student.Birth);
                    Console.WriteLine("电话号码：" + student.Mobile);
                    Console.WriteLine("年龄：" + student.Age);
                }
            }
            //得到最低分的信息
            int min = students.Min(m => m.Grade);
            foreach (Student student in students)
            {
                if (student.Grade == min)
                {
                    Console.WriteLine("最低分同学的姓名：" + student.Name);
                    Console.WriteLine("学号：" + student.No);
                    Console.WriteLine("成绩：" + student.Grade);
                    Console.WriteLine("出生日期：" + student.Birth);
                    Console.WriteLine("电话号码：" + student.Mobile);
                    Console.WriteLine("年龄：" + student.Age);
                }
            }

        }
        /// <summary>
        /// 读取excel文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static void ReadExl(string path, List<Student> students)
        {
            FileStream stream = new FileStream(path, FileMode.Open);
            //获取Excel
            XSSFWorkbook workbook = new XSSFWorkbook(stream);
            //获取文件的sheet
            XSSFSheet worksheet = workbook.GetSheet("Sheet1") as XSSFSheet;
            //开始逐行导入Excel的数据
            for (int i = 1; i < worksheet.LastRowNum; i++)
            {
                if (i == 0)
                    continue;
                Student stu = new Student();
                IRow row = worksheet.GetRow(i);
                for (int j = 0; j < row.LastCellNum; j++)
                {

                    ICell cell = row.GetCell(j);
                    switch (j)
                    {
                        case 0:
                            {
                                stu.Name = Convert.ToString(cell);
                                break;
                            }
                        case 1:
                            {
                                stu.No = Convert.ToString(cell);
                                break;
                            }
                        case 2:
                            {
                                stu.Mobile = Convert.ToString(cell);
                                break;
                            }
                        case 3:
                            {
                                string s = Convert.ToString(cell);
                                stu.Grade = Convert.ToInt32(s);
                                break;
                            }
                        case 4:
                            {
                                stu.Birth = DateTime.Parse(Convert.ToString(cell));
                                break;
                            }
                    }

                }
                students.Add(stu);
            }
        }
        /// <summary>
        /// 读取txt文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<Student>ReadFromTxt(string path)
        {
            List<Student> students = new List<Student>();
            //得到文件内容
            string text=File.ReadAllText(path);
            string[] lines = text.Split('\n');
            string[][]words=new string[lines.Length-1][];
            for (int i=1; i<lines.Length-1; i++)
            {
                words[i]=lines[i].Split(' ');
                var student=new Student();
                //Student student=new Student(words[i][0],words[i][1],words[i][2],words[i][3],words[i][4]);
                for (int j = 0; j < words[i].Length; j++)
                {
                    switch (j)
                    {
                        case 0:
                            {
                                student.Name = words[i][j];
                                break;
                            }
                        case 1:
                            {
                                student.No = words[i][j];
                                break;
                            }
                        case 3:
                            {
                                student.Grade = Convert.ToInt32(words[i][j]);
                                break;
                            }
                        case 4:
                            {
                                student.Birth = Convert.ToDateTime(words[i][j]);
                                break;
                            }
                        case 2:
                            {
                                student.Mobile = words[i][j];
                                break;
                            }
                    }
                }
                students.Add(student);
            }
            
            return students;
        }
    }
}
